home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 64 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Reclaiming memory allocated recursively
  5. Date: 1 Jan 1996 13:20:00 GMT
  6. Organization: Kalevi, Inc
  7. Message-ID: <4c8n20$6k2@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: pipe5.h1.usa.pipeline.com
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Jan 02, 1996 02:52:35 in article <Reclaiming memory allocated
  15. recursively>, 'bvarley@yarrow.wt.com.au (bruce varley)' wrote: 
  16.  
  17. The code presented seems OK in concept; however, there's 
  18. some places that look suspicious without seeing all of the code. 
  19.  
  20. >I'm using self-referential structures as described in K&R 6.5 (page 
  21. >130 in my version). The program runs 'forever',  storing data in a 
  22. >binary tree - a days worh of data is collected, then dumped, and the 
  23. >whole process starts all over again. My question is:  How can I 
  24. >reclaim the memory that has been recursively allocated?  Using the 
  25. >same approach as creating the tree  - ie.......... 
  26. >    struct node *freetree (struct node *p)  
  27. Just a comment:  Functionally, freetree might as well be of type void 
  28. since the return value is always 0; i.e., meaningless.  
  29. >            { 
  30. >            if (p != NULL)   
  31. Does your code guarantee that p (i.e., left and right nodes) are NULL 
  32. at the leaves?  If not, a crash could result. 
  33. >                { 
  34. >                freetree (p -> left)   ; 
  35. >                free (p -> data_pointer) ; 
  36. Is data_pointer a unique pointer?  In other words, could some other 
  37. pointer possibly also point to the same data causing a memory block 
  38. to be freed multiply? 
  39. >                freetree (p -> right)  ; 
  40. >                } 
  41. >            return (0)  ; 
  42. >            } 
  43. >seems unlikely to work, because the memory required to navigate 
  44.  
  45. Why unlikely?  Makes sense to me -- or am I missing something? 
  46.  
  47. >through the tree is being deallocated as the process proceeds. In 
  48. >fact, the system crashes when I run the routine. 
  49.  
  50. On another note, your code does not seem to release the memory for 
  51. the tree nodes themselves.  If you run this a lot, then you could crash 
  52. due to running out of memory.  Try sticking a "free(p);" after  
  53. freeing the left and right nodes. 
  54.  
  55. -- 
  56.  
  57. Pete 
  58.  
  59.  
  60.  
  61.  
  62.  
  63.